﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using Caliburn.Autofac;
using Autofac;
using Microsoft.Practices.ServiceLocation;
using IronCommander.ViewModels;
using IronCommander.Domain;
using Caliburn.PresentationFramework.ApplicationModel;

namespace IronCommander
{
    public partial class App 
    {

        protected override object CreateRootModel()
        {
            var binder = (DefaultBinder)Container.GetInstance<IBinder>();
            //binder.EnableBindingConventions();
            //binder.EnableMessageConventions();

            return Container.GetInstance<MainViewModel>();
        }

        protected override IServiceLocator CreateContainer()
        {
            return new AutofacAdapter(BuildContainer());
        }

        private IContainer BuildContainer()
        {
            var builder = new ContainerBuilder();

            builder.RegisterAssemblyTypes(typeof(MainViewModel).Assembly).Where(v => v.FullName.Contains(".ViewModels"));
            builder.RegisterAssemblyTypes(typeof(MainViewModel).Assembly).Where(v => v.FullName.Contains(".Views"));
            builder.RegisterType<CurrentPaths>().SingleInstance();
            builder.RegisterType<MainViewModel>().SingleInstance();

            builder.RegisterType<FileSystemInfoProvider>().As<IFileSystemInfoProvider>();
            //builder.RegisterType<MockFileSystemInfoProvider>().As<IFileSystemInfoProvider>();

            return builder.Build();
        }
    }

    class MockFileSystemInfoProvider : IFileSystemInfoProvider
    {
        public IEnumerable<DriveViewModel> GetDrives()
        {
            return new[] { new DriveViewModel { DriveName = @"Z:\" }, new DriveViewModel { DriveName = @"Y:\" } };
        }

        public IEnumerable<FileViewModel> GetFiles(string path)
        {
            return Enumerable.Range(0, 100000).Select(f => new FileViewModel
            {
                DisplayName = string.Format("File {0}", f),
                FullName = string.Format(path + @"\File {0}", f),
                IsFolder = true
            });
        }
    }
}
